package grammar.parser.stmt;

import error.processing.MyErrorProcessing;
import exp.parser.LVal;
import word.parser.WordElement;

public abstract class Assign extends Stmt {
    private final LVal lVal;

    public Assign(LVal lVal) {
        this.lVal = lVal;
    }

    public static Assign buildAssignStmt(LVal lVal) {

        /*
            左值判断
        */
        if (lVal.isConst()) {
            MyErrorProcessing.buildErrorProcessing(MyErrorProcessing.ErrorType.h,
                    getAnalyzingWord(-1));
        }


        Assign assign;
        assert getAnalyzingWord().getType() == WordElement.WordType.ASSIGN;
        analyzeNextWord();

        if (getAnalyzingWord().getType() == WordElement.WordType.GETINTTK) {
            assign = GetIntAssign.buildGetIntAssign(lVal);
        } else {
            assign = ExpAssign.buildExpAssign(lVal);
        }

        if (getAnalyzingWord().getType() != WordElement.WordType.SEMICN) {
            MyErrorProcessing.buildErrorProcessing(MyErrorProcessing.ErrorType.i,
                    getAnalyzingWord(-1));
        } else {
            analyzeNextWord();
        }
        return assign;
    }

    public LVal getlVal() {
        return lVal;
    }
}
